package com.mycookbook;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ApproachFragment extends Fragment {
	
	static DatabaseHandler db;
	private static String text = "???";
	static ListView listview;
	static TextView textview;
	private static final String KEY_TEXT = "ApproachFragment:Text";
	static ArrayAdapter<String> aa;
	static String[] approach;
	static Context context;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
	     
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		 ApplicationData d = ((ApplicationData)activity.getApplicationContext());
	     db = d.GetDatabase();
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		if (savedInstanceState != null) {
	        // Restore last state for checked position.
	    	
	    	text = savedInstanceState.getString(KEY_TEXT);
	    	
	          
        }
	}

	// update Approach
	public static void textUpdateApproach(Recipe data){
		
		textview.setText(data.getName() + "\n approach");
		approach= db.getApproachAsArray(data.getName());
		
		aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, approach);
	    listview.setAdapter(aa);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.ingredientfragment, container, false);
		
		listview = (ListView) view.findViewById(R.id.listView1);
		textview = (TextView) view.findViewById(R.id.textViewRecipeTitle);
		
		textview.setText(text + "\n approach");
		
		 approach= db.getApproachAsArray(text);
		
		 context = this.getActivity();
		 
		 aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, approach);
	     listview.setAdapter(aa);
		
		return view;
	}
	
	public static ApproachFragment newInstance(String text) {
		ApproachFragment fragment = new ApproachFragment();  
        
        fragment.text = text;     
	
        return fragment;
    }

}
